/**
 * “我的荣耀”基础逻辑
 * */
import http from '../../libs/request.js'
import dictMixins from '../../libs/dictMixins.js'
export default {
	name: 'gloryMixins',
	mixins: [dictMixins],
	data() {
		return {
			applyData: [],
		};
	},
	methods: {
		// 列表相关
		async fetchApprovalData(params) {
			let resp = await http.post({url: this.pageUrl ,data: params});
			if (resp && resp.result) {
				this.applyData = resp.result.rows.map(item => {
					item.applyStatusName = this.getDictNameWithCodeAndValue('dict_apply_status', item.applyStatus);
					item.icon = `/static/applyStatus_${item.applyStatus}.png`
					return item;
				});
			}
		},
		toDetail(item, type) {
			uni.navigateTo({
				url: `${this.modulePre}${type}?info=${JSON.stringify({item})}`
			})
		},
		// 申请相关
		toReqModel(model, callback) {
			let reqModel = Object.assign({},model,{});
			delete reqModel.fileList;
			this.formConfig.props.map(propItem => {
				let modelItem = model[propItem.prop];
				if (propItem.valueKey) {
					reqModel[propItem.prop] = modelItem[propItem.valueKey];
				}
				if (propItem.dictCode) {
					reqModel[propItem.prop] = modelItem.value;
				}
			})
			reqModel.attachmentVoListStr = JSON.stringify(model.fileList.map(item => {
				let obj = {
					docFileId: item.fileItem.id,
					docFileName: item.fileItem.fileName,
					docFileUrl: item.fileItem.fileUrl
				};
				return obj;
			}));
			callback && callback(reqModel);
		},
		async submitApply(params) {
			let resp = await this.$http.post({url: this.applyUrl ,data: params});
			uni.navigateBack();
		},
		// 详情相关
		assignDetail() {
			this.info = JSON.parse(this.getPageOptions().info);
			// 赋值
			let model = this.info.item;
			Object.keys(model).map(key => {
				let propItem = this.formConfig.props.find(item => { return item.prop == key });	
				if (propItem && propItem.valueKey) {	// label、value 合并
					model[key] = {
						label: model[key],
						value: model[key]
					};
				}
				if (propItem && propItem.dictCode) {
					model[key] = {
						label: this.getDictNameWithCodeAndValue(propItem.dictCode, model[key]),
						value: model[key]
					};
				}
			})
			if (model.attachmentVoListStr) {
				model.fileList = (JSON.parse(model.attachmentVoListStr)).map(item => {
					item.url = item.fileUrl;
					return item;
				})
			}
			this.formConfig.model = model;
		}
	},
	computed: {
		noApplyData() {
			return this.applyData.length == 0
		},
	}
}